package com.jm.common.util;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Component;

/**
 * 基于ThreadLocal封装的工具类,用于保存与获取当前登录的用户的Id
 * 它的作用范围为一个线程之内,其用于设置与取出数据
 *
 *
 */
@Slf4j
@Component
public class Utils {
    static {
        tl = new ThreadLocal<>();
    }

    private static final ThreadLocal<Long> tl;

    public static void setCurrentId(Long id) {
        tl.set(id);
    }

    public static Long getCurrentId() {
        return tl.get();
    }

    /**
     * 用于存放缓存key为手机号
     *
     * @param phone
     * @return
     */
    @CachePut(value = "phoneCode", key = "#phone" )
    public String setPhoneCodeCache(String phone) {
        int i = RandomUtil.randomInt(100000,999999);
        log.info("生成手机号的{}的验证码为{}", phone, i);
        return i + "";
    }

    /**
     * 取出缓存
     *
     * @param phone
     * @return
     */
    @Cacheable(value = "phoneCode", key = "#phone")
    public String getPhoneCodeCache(String phone) {
        return null;
    }



}
